package Entities;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import Abilities.Ability;
import EventHandling.Timer;

public class Entity {

	private List<Ability> deck = new ArrayList<Ability>();
	private Timer timer;
	private EntityType type;

	//values that are taken from log
	private String name;
	private int hits;
	private int hitsTaken;
	private int crits;
	private int pens;
	private int blocks;
	private int glances;
	private int blocksTaken;
	private int glancesTaken;
	private int pensTaken;
	private int critsTaken;

	//calculated values
	private double damagePerSecond = 0; 
	private double healsPerSecond = 0;

	//variables to store the current amount of damage being done or taken
	private double currentDamageTaken;
	private double currentDamageDone;
	private double currentHealingDone;
	private double currentHealingReceived;

	DecimalFormat df = new DecimalFormat("##.##");
	
	//CONSTRUCTORS
	public Entity() {	
		this.name = "";
		this.hits = 0;
		this.hitsTaken = 0;
		this.blocks = 0;
		this.crits = 0;
		this.glances = 0;
		this.pens = 0;
		this.blocksTaken = 0;
		this.glancesTaken = 0;
		this.pensTaken = 0;
		this.critsTaken = 0;
		this.currentDamageDone = 0;
		this.currentDamageTaken = 0;
		this.currentHealingDone = 0;
		this.currentHealingReceived = 0;
		deck = new ArrayList<Ability>();
		timer = new Timer();
	}

	public Entity(String aName, EntityType type) {	
		this();
		this.type = type;
		this.name = aName;
	}
	
	//constructor used for targets
	public Entity(String aName, EntityType type, int eventAmount) {	
		this();
		this.type = type;
		this.name = aName;
		this.currentDamageTaken = eventAmount;
	}
	
	public Entity(String aName, EntityType type, String abilityName, int amount) {
		this(aName, type);
		this.addAbilityToDeck(abilityName, amount);
	}
	
	public Entity(String aName, EntityType type, String abilityName, int damage, int time) {
		this(aName, type, abilityName, damage);
		this.timer.setStartTime(time);
	}

	//CLASS METHODS
	public void addAbilityToDeck(String abilityName, int amount) {
		Ability ability = null;
		int index = indexIfAbilityFound(abilityName);
		if (index > -1) {
			ability = deck.get(index);
			ability.setTotalAmount(amount);
		} else {
			ability = new Ability(abilityName, amount);
			deck.add(ability);
		}
	}

	public int indexIfAbilityFound(String abilityName) {
		int index = -1;
		for (int i = 0; i < deck.size(); i++) {
			if (deck.get(i).getName().equalsIgnoreCase(abilityName)) {
				index = i;
			}
		}
		return index;
	}

	public double calculatePercentage(double noOfSpecialHits, int noOfHits) {
		double percentage = noOfSpecialHits/noOfHits;
		if (percentage <= 0) {
			return 0.00;
		} else
			return percentage * 100;
	}

	public void calculateDamagePerSecond(double elapsedTime, double damageDone) {
		if (elapsedTime < 1) {
			elapsedTime = 1;
		}
		this.damagePerSecond = (damageDone / elapsedTime);
	}
	
	public void calculateHealsPerSecond(double elapsedTime, double healingDone) {
		if (elapsedTime < 1) {
			elapsedTime = 1;
		}
		this.healsPerSecond = (healingDone / elapsedTime);
	}

	
	
	//SETTERS
	public void setName(String name) {
		this.name = name;
	}

	public void setType(EntityType type) {
		this.type = type;
	}
	
	public void setCurrentDamageTaken(int amount) {
		this.currentDamageTaken += amount;
	}
	
	public void setCurrentHealingDone(int amount) {
		this.currentHealingDone += amount;
	}
	
	public void setCurrentDamageDone(int amount) {
		this.currentDamageDone += amount;
	}
	
	public void setCurrentHealingReceived(int eventAmount) {
		this.currentHealingReceived += eventAmount;
		
	}
	
	//GETTERS
	public EntityType getType() {
		return type;
	}
	
	public List<Ability> getDeck() {
		return deck;
	}

	public Timer getTimer() {
		return timer;
	}

	public String getName() {
		return name;
	}

	public int getHits() {
		return hits;
	}

	public int getHitsTaken() {
		return hitsTaken;
	}

	public int getCrits() {
		return crits;
	}

	public int getPens() {
		return pens;
	}

	public int getBlocks() {
		return blocks;
	}

	public int getGlances() {
		return glances;
	}

	public int getBlocksTaken() {
		return blocksTaken;
	}

	public int getGlancesTaken() {
		return glancesTaken;
	}

	public int getPensTaken() {
		return pensTaken;
	}

	public int getCritsTaken() {
		return critsTaken;
	}

	public double getDamagePerSecond() {
		return damagePerSecond;
	}

	public double getHealsPerSecond() {
		return healsPerSecond;
	}

	public double getCurrentDamageTaken() {
		return currentDamageTaken;
	}

	public double getCurrentDamageDone() {
		return currentDamageDone;
	}

	public double getCurrentHealingDone() {
		return currentHealingDone;
	}

	public double getCurrentHealingReceived() {
		return currentHealingReceived;
	}

	@Override
	public String toString() {
		return "Player:\t" + getName() + "\tDeck:\t" + getDeck() + "\nDamage Done:\t" + getCurrentDamageDone();
	}

}
